<?php

namespace App\Http\Controllers\Admin;

use App\Http\Services\AdminService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdminController extends Controller
{

    /**
     *管理员删除
     * @author lican<364548233@qq.com>
     */
    public function delete(Request $request){
        $this->validate($request, [
            'admin' => 'required',
            'id' => 'required|int'
        ]);
        $admin = $request->input('admin');
        $id = (int)$request->input('id');
        $res = AdminService::delete($admin, $id);
        return $res;
    }

    /**
     *管理员状态切换
     * @author lican<364548233@qq.com>
     */
    public function changeStatus(Request $request){
        $this->validate($request, [
            'admin' => 'required',
            'id' => 'required|int'
        ]);
        $admin = $request->input('admin');
        $id = (int)$request->input('id');
        $res = AdminService::changeStatus($admin, $id);
        return $res;
    }

    /**
     *管理员添加
     * @author lican<364548233@qq.com>
     */
    public function addAdmin(Request $request){
        $this->validate($request, [
            'admin' => 'required',
            'name' => 'required|between:2,8',
            'password' => 'required|between:6,16',
            'role_id' => 'required|int',
            'email' => 'required',
            'mobile' => 'required'
        ]);
        $admin = $request->input('admin');
        $name = trim($request->input('name'));
        $password = trim($request->input('password'));
        $headImage = trim($request->input('head_image'));
        $roleId = (int)$request->input('role_id');
        $email = trim($request->input('email'));
        $mobile = trim($request->input('mobile'));
        $id = (int)$request->input('id');
        $res = AdminService::saveAdmin($admin, $name, $password, $headImage, $roleId, $mobile, $email, $id);
        return $res;
    }

    /**
     *管理员列表
     * @author: lican<364548233@qq.com>
     */
    public function index(Request $request){
        $this->validate($request, [
            'admin' => 'required',
        ]);
        $admin = $request->input('admin');
        $keyword = trim($request->input('keyword')) ?? '';
        $page = (int)$request->input('page') ?? 1;
        $pageSize = (int)$request->input('pageSize') ?? 10;
        $res = AdminService::index($keyword, $page, $pageSize);
        return $res;
    }

    /**
     *获取管理员
     * @author: lican<364548233@qq.com>
     */
    public function getAdmin(Request $request){
        $this->validate($request, [
            'admin' => 'required'
        ]);
        $res = AdminService::getAdmin();
        return $res;
    }

}
